package com.tanhua.dubbo.api;

import com.tanhua.model.mongo.UserLike;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class UserlikeApiImpl implements UserlikeApi{

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 查询我喜欢的UserLike集合
     */
    @Override
    public List<UserLike> findLoveIdsByUserId(Long userId) {
        Criteria criteria = Criteria.where("userId").is(userId).and("isLike").is(true);
        Query query = Query.query(criteria);
        List<UserLike> list = mongoTemplate.find(query, UserLike.class);
        return list;
    }

    /**
     * 查询喜欢我的UserLike集合
     */
    @Override
    public List<UserLike> findFanIdsByUserId(Long userId) {
        Criteria criteria = Criteria.where("likeUserId").is(userId).and("isLike").is(true);
        Query query = Query.query(criteria);
        List<UserLike> list = mongoTemplate.find(query, UserLike.class);
        return list;
    }

    /**
     * 查询互相喜欢的UserLike集合
     */
    @Override
    public List<UserLike> findEachLoveIdsByUserId(Long userId) {
        //先查询出我喜欢的
        List<UserLike> loveList = findLoveIdsByUserId(userId);
        //在查询出喜欢我的
        List<UserLike> fanList = findFanIdsByUserId(userId);
        //进行判断,得到集合
        List<UserLike> list = new ArrayList<>();
        for (UserLike userLike : loveList) {
            for (UserLike like : fanList) {
                Long likeUserId = userLike.getLikeUserId();
                if (likeUserId == like.getUserId()){
                    list.add(userLike);
                }
            }
        }
        return list;
    }

    /**
     * 分页查询互相喜欢的
     */
    @Override
    public List<UserLike> findEachLoveByPage(Long userId, Integer page, Integer pagesize) {
        List<UserLike> loveList = findLoveIdsByUserId(userId);
        List<Long> ids = new ArrayList<>();
        for (UserLike userLike : loveList) {
            ids.add(userLike.getLikeUserId());
        }
        System.out.println(ids+"=========================");
        Criteria criteria = Criteria.where("userId").in(ids)
                .and("likeUserId").is(userId)
                .and("isLike").is(true);
//        Criteria criteria1 = Criteria.where("$where").is("this.userId == this.likeUserId").and("userId").is(userId);
        Query query = Query.query(criteria)
                .with(Sort.by(Sort.Order.desc("created")))
                .skip((page-1)*pagesize)
                .limit(pagesize);
        List<UserLike> list = mongoTemplate.find(query, UserLike.class);
        System.out.println(list+"=======================");
        return list;
    }

    /**
     * 分页查询我喜欢的
     */
    @Override
    public List<UserLike> findLoveByPage(Long userId, Integer page, Integer pagesize) {
        Criteria criteria = Criteria.where("UserId").is(userId).and("isLike").is(true);
        Query query = Query.query(criteria)
                .with(Sort.by(Sort.Order.desc("created")))
                .skip((page-1)*pagesize)
                .limit(pagesize);
        List<UserLike> list = mongoTemplate.find(query, UserLike.class);
        return list;
    }

    /**
     * 分页查询喜欢我的
     */
    @Override
    public List<UserLike> findFanByPage(Long userId, Integer page, Integer pagesize) {
        Criteria criteria = Criteria.where("likeUserId").is(userId).and("isLike").is(true);
        Query query = Query.query(criteria)
                .with(Sort.by(Sort.Order.desc("created")))
                .skip((page-1)*pagesize)
                .limit(pagesize);
        List<UserLike> list = mongoTemplate.find(query, UserLike.class);
        return list;
    }

    /**
     * 设置喜欢
     */
    @Override
    public void setIsLike(Long uid, Long userId) {
        Criteria criteria = Criteria.where("userId").is(userId).and("likeUserId").is(uid);
        Query query = Query.query(criteria);
        UserLike userLike = mongoTemplate.findOne(query, UserLike.class);
        Update update = new Update().set("isLike",!userLike.getIsLike());
        mongoTemplate.updateFirst(query,update,UserLike.class);
    }

    @Override
    public void fanLike(String uid, Long userId) {

    }

    //查询islike的值是否当前用户喜欢
    @Override
    public Boolean findIsFriend(Long userId, Long friendId) {
        UserLike userLike = mongoTemplate.findOne(Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(friendId)), UserLike.class);
        if (userLike == null){
            return false;
        }
        return userLike.getIsLike();
    }

    //查询是否存在
    @Override
    public Boolean isExist(Long userId, Long friendId) {
        Criteria criteria = Criteria.where("userId").is(userId).and("likeUserId").is(friendId);
        Query query = Query.query(criteria);
        boolean exists = mongoTemplate.exists(query, UserLike.class);
        return exists;
    }

    //添加喜欢关系
    @Override
    public void save(Long userId, Long friendId) {
        UserLike userLike = new UserLike();
        userLike.setId(new ObjectId());
        userLike.setIsLike(true);
        userLike.setUserId(userId);
        userLike.setLikeUserId(friendId);
        userLike.setCreated(System.currentTimeMillis());
        mongoTemplate.save(userLike);
    }

    @Override
    public Boolean isExisit(Long userId, Long likeUserId) {
        Query query = new Query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
        return mongoTemplate.exists(query,UserLike.class);

    }
    //保存喜欢数据到user_like表中
    @Override
    public void saveLike(Long userId, Long likeUserId) {
        Criteria criteria = Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId);
        Query query = Query.query(criteria);
        if (!mongoTemplate.exists(query,UserLike.class)){
            UserLike userLike = new UserLike();
            userLike.setUserId(userId);
            userLike.setLikeUserId(likeUserId);
            userLike.setIsLike(true);
            userLike.setCreated(System.currentTimeMillis());
            userLike.setUpdated(System.currentTimeMillis());
            mongoTemplate.save(userLike);
        }
        setIsLike(likeUserId, userId);
    }

    //是否互相喜欢(两个参数各自对应的)
    @Override
    public Boolean isLike(Long likeUserId, Long userId) {
        Criteria criteria = Criteria.where("likeUserId").is(userId).and("userId").is(likeUserId);
        Query query = Query.query(criteria);
        return mongoTemplate.exists(query,UserLike.class);
    }
}
